package com.qsmx.store.app.main.gift.adapter.gift;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.qsmx.store.app.R;
import com.qsmx.store.app.main.gift.entity.GiftEntity;
import com.qsmx.store.app.main.gift.entity.GiftGoodsEntity;
import com.qsmx.store.app.main.goods.adapter.GoodsOrderAdapter;
import com.qsmx.store.core.util.dimen.DimenUtil;

import java.util.ArrayList;
import java.util.List;

public class GiftContentAdapter extends ArrayAdapter {

    private int itemResourceId;
    private List<GiftEntity> mGiftList;
    private List<GiftGoodsEntity> giftContentList = new ArrayList<>();

    public void setList(List<GiftEntity> mGiftList) {
        this.mGiftList = mGiftList;
    }

    public GiftContentAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);
        this.itemResourceId = resource;
    }

    @Override
    public int getCount() {
//            if (coinsList != null && coinsList.size() > 0) {
//                return coinsList.size();
//            } else {
//                return 0;
//            }

        return 20;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

//            MemCoinsEntity data = coinsList.get(position);

        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(itemResourceId, parent, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.tvOrderDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOrderDetailDialog(false);
            }
        });

        viewHolder.tvBackMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOrderDetailDialog(true);
            }
        });

        return view;
    }

    private class ViewHolder {
        private TextView tvOrderDetail;
        private TextView tvBackMoney;

        ViewHolder(View itemView) {
            tvOrderDetail = (TextView) itemView.findViewById(R.id.tv_order_detail);
            tvBackMoney = itemView.findViewById(R.id.tv_back_money);
        }
    }

    private void showOrderDetailDialog(boolean isBack) {
        final Dialog dialog = new Dialog(getContext(), R.style.dialog);
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_gift_detail);
        int width = (int) (DimenUtil.getScreenWidth() * 0.8);

        window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

        initContentDataList();
        ConstraintLayout clBottom = window.findViewById(R.id.cl_bottom_content);
        RecyclerView rlvGoodsXls = window.findViewById(R.id.rlv_goods_xls);
        GoodsOrderAdapter mGoodsAdapter = new GoodsOrderAdapter(getContext());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        rlvGoodsXls.setAdapter(mGoodsAdapter);
        rlvGoodsXls.setLayoutManager(manager);

        if (isBack) {
            clBottom.setVisibility(View.VISIBLE);
        } else {
            clBottom.setVisibility(View.GONE);
        }

    }

    private void initContentDataList() {

        giftContentList = new ArrayList<>();
        for (int i = 0; i <= 20; i++) {
            GiftGoodsEntity doInfo = new GiftGoodsEntity();
            giftContentList.add(doInfo);
        }

    }
}
